﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System.IO;
using System.Diagnostics;
using IronPython.Runtime;


namespace CSharpCallPython
{
    class Program
    {
        static void Main(string[] args)
        {
            string pythonFileName = "PClass.py";
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start(); //  开始监视代码运行时间   
            ScriptRuntime sr = Python.CreateRuntime();
            ScriptScope scope = sr.UseFile(pythonFileName);
            PythonFunction pf = scope.GetVariable<PythonFunction>("say");
            ScriptEngine engine = scope.Engine;

            if (engine.Operations.IsCallable(pf))
                Console.WriteLine(engine.Operations.Invoke(pf, "Hello Python!!!!"));

            //CodeContext context = new CodeContext(
            //pf.__call__(
            //dynamic d = scope.GetVariable("say");


            for (int i = 0; i < 1000; i++)
            {
                //Console.WriteLine(d("Hello Python!"));
                //Console.WriteLine(scope.GetVariable("test")());
                if (engine.Operations.IsCallable(pf))
                    Console.WriteLine(engine.Operations.Invoke(pf, "Hello Python!!!!"));
            }
            //PythonDictionary dic = new PythonDictionary();




            stopwatch.Stop(); //  停止监视           
            TimeSpan timespan = stopwatch.Elapsed; //  获取当前实例测量得出的总时间               
            double milliseconds = timespan.TotalMilliseconds;  //  总毫秒数
            Console.WriteLine(milliseconds);
            Console.ReadKey();
        }
    }
}
